/**
 * Created by DevilsEyes on 2016/2/13.
 * ================================
 *             初始化
 * ================================
 *
 * 初始化时，决定是否向待搜索列表添加任务。
 * 添加任务的算法。
 */
'use strict';
var $doc = require('../model/doc.js');
var $stats = require('../model/stats.js');
var config = require('../unit/config.js');

//$stats.clear();
//$doc.clear();

var stats = null;
var initMax = config.initMax;
let keywords = [
    'the austerity of love',
    '歌词 the austerity of love',
    'lyric the austerity of love'
];

module.exports = {
    done: (callback) => {
        $stats.count(null, (err, res) => {
            stats = res;
            console.log(stats);
            console.log('\n================\n开始运作\n================\n');

            if (stats.status == 0) {
                var i = 0, count = initMax;
                for (i = 0; i < initMax; i++) {

                    for (let keyword of keywords) {
                        let link = encodeURI(`http://www.baidu.com/s?wd=${keyword}&ie=utf-8&pn=${i * 10}`);
                        $doc.save({
                            link: link,
                            deep: 0,
                            root: 'B' + i,
                            status: 0,
                            path: link
                        }).then((err, doc) => {
                            count--;
                            if (count == 0) {
                                callback(stats);
                            }
                        })
                    }

                }
            } else {
                callback(stats);
            }
            //...
        });
    }
};